import os
import urllib
import urllib2

import bs4
import requests

agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) ' \
        'AppleWebKit/537.36 (KHTML, like Gecko)Chrome/57.0.2987.98 Safari/537.36'
header={
    'User-Agent':agent,
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch, br',
    'Accept-Language':'h-CN,zh;q=0.8,en;q=0.6'
}
i=1
def getPhoto(url,path):
    if not os.path.exists(path):
        os.makedirs(path)
    global i
    filename=path+'/'+str(i)+os.path.splitext(url)[1]
    i=i+1
    img=urllib.urlopen(url).read()
    f=file(filename,'wb')
    f.write(img)
    f.close()

def download(url):
    f=requests.get(url,headers=header)
    soup= bs4.BeautifulSoup(f.text,'lxml')
    lists=soup.find('body').find('div',{'id':'root'}).find('div').find('main',{'role':'main'}).find('div').find('div',{'class':'Question-main'})\
        .find('div',{'class':'Question-mainColumn'}).find('div').find('div',{'class':'Card'}).find('div',{'class':'List'}).findAll('div',{'class':'List-item'})
    for list in lists:
        imgurls=list.find('div',{'class':'ContentItem'}).find('div',{'class':'RichContent RichContent--unescapable'}).find('div',{'class':'RichContent-inner'})\
            .find('span',{'class':'RichText CopyrightRichText-richText'}).findAll('img')
        for img in imgurls:
            if 'data-original' in img.attrs.keys():
                src=img.attrs['data-original']
            elif 'data-actualsrc' in img.attrs.keys():
                src=img.attrs['data-actualsrc']
            elif 'src' in img.attrs.keys():
                src=img.attrs['src']
            else :break
            getPhoto(src,'img')



if __name__=='__main__':
    for line in open('url','r'):
        download(line.strip())